package com.qf.web.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String m = req.getParameter("method");

        try {
            Method method = this.getClass().getMethod(m, HttpServletRequest.class, HttpServletResponse.class);
            if (method!=null){
                String URL= (String) method.invoke(this,req,resp);
                if (URL==null||URL.trim().length()==0){
                    return;
                }else{
                    if (URL.startsWith("redirect:")){
                        resp.sendRedirect(req.getContextPath()+URL.substring(URL.lastIndexOf(":")+1));
                    }else{
                        req.getRequestDispatcher(URL).forward(req,resp);
                    }
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
